package com.faradel.pharmapp.managedbean;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.faradel.pharmapp.entities.Patient;
import org.faradel.pharmapp.entities.PatientOrder;
import org.faradel.pharmapp.entities.PrescriptionOrder;
import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.faradel.pharmapp.service.MedicationInventoryServiceInterface;
import com.faradel.pharmapp.service.MedicationServiceInterface;
import com.faradel.pharmapp.service.PatientOrderServiceInterface;
import com.faradel.pharmapp.service.PatientServiceInterface;
import com.faradel.pharmapp.service.PrescriptionOrderServiceInterface;

@ManagedBean
@ViewScoped
public class ProcessOrderBean implements Serializable
{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String orderId;

    @Resource
    PatientOrder patientOrder;

    @Resource
    Patient patient;

    private BigDecimal total;

    private String paymentMethod;

    private List<PrescriptionOrder> prescriptionList;

    {
        Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();

        orderId = (String) sessionMap.get("orderId");

        patientOrder = getPatientOrderServiceInterface().getPatientOrderById(Integer.parseInt(orderId));
        patient = getPatientServiceInterface().getPatientById(patientOrder.getPatientId());

        prescriptionList = patientOrder.getPrescriptionOrders();
        total = calculateTotals(prescriptionList);
    }

    /**
     * Get the total medication costs
     * 
     * @param pl
     * @return
     */
    private BigDecimal calculateTotals(List<PrescriptionOrder> pl)
    {
        BigDecimal retval = new BigDecimal("0.0");
        for (PrescriptionOrder po : pl)
        {
            retval = retval.add(po.getCost().multiply(new BigDecimal(po.getQuantity())));
        }
        return retval;
    }

    private PatientServiceInterface getPatientServiceInterface()
    {
        ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        return (PatientServiceInterface) context.getBean("patientServiceImpl");
    }

    private PatientOrderServiceInterface getPatientOrderServiceInterface()
    {
        ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        return (PatientOrderServiceInterface) context.getBean("patientOrderServiceImpl");
    }

    private PrescriptionOrderServiceInterface getPrescriptionOrderServiceInterface()
    {
        ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        return (PrescriptionOrderServiceInterface) context.getBean("prescriptionOrderServiceImpl");
    }

    private MedicationServiceInterface getMedicationServiceInterface()
    {
        ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        return (MedicationServiceInterface) context.getBean("medicationServiceImpl");
    }

    private MedicationInventoryServiceInterface getMedicationInventoryServiceInterface()
    {
        ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        return (MedicationInventoryServiceInterface) context.getBean("medicationInventoryServiceImpl");
    }

    public String getOrderId()
    {
        return orderId;
    }

    public void setOrderId(String orderId)
    {
        this.orderId = orderId;
    }

    public List<PrescriptionOrder> getPrescriptionList()
    {
        return prescriptionList;
    }

    public void setPrescriptionList(List<PrescriptionOrder> prescriptionList)
    {
        this.prescriptionList = prescriptionList;
    }

    public PatientOrder getPatientOrder()
    {
        return patientOrder;
    }

    public void setPatientOrder(PatientOrder patientOrder)
    {
        this.patientOrder = patientOrder;
    }

    public Patient getPatient()
    {
        return patient;
    }

    public void setPatient(Patient patient)
    {
        this.patient = patient;
    }

    public BigDecimal getTotal()
    {
        return total;
    }

    public void setTotal(BigDecimal total)
    {
        this.total = total;
    }

    public String getPaymentMethod()
    {
        return paymentMethod;
    }

    public void setPaymentMethod(String paymentMethod)
    {
        this.paymentMethod = paymentMethod;
    }
}
